package com.woniu.outpatient.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.woniu.outpatient.domain.Drugtype;
import com.woniu.outpatient.service.IDrugtypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.woniu.outpatient.domain.Drug;
import com.woniu.outpatient.service.IDrugService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 药品库存表Controller
 *
 * @author wolf-six
 * @date 2023-02-15
 */
@RestController
@RequestMapping("/drug")
public class DrugController extends BaseController
{
    @Autowired
    private IDrugService drugService;

    @Autowired
    private IDrugtypeService drugtypeService;

    /**
     * 查询药品库存表列表
     */
    @RequiresPermissions("outpatient:drug:list")
    @GetMapping("/list")
    public TableDataInfo list(Drug drug)
    {
        startPage();
        List<Drug> list = drugService.selectDrugList(drug);
        for (Drug drug1 : list) {
            Long drugtypeId = drug1.getDrugtypeId();
            Drugtype drugtype = drugtypeService.selectDrugtypeByDrugtypeId(drugtypeId);
            drug1.setDrugtypeName(drugtype.getDrugtypeName());
        }

        return getDataTable(list);
    }

    /**
     * 导出药品库存表列表
     */
    @RequiresPermissions("outpatient:drug:export")
    @Log(title = "药品库存表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Drug drug)
    {
        List<Drug> list = drugService.selectDrugList(drug);
        ExcelUtil<Drug> util = new ExcelUtil<Drug>(Drug.class);
        util.exportExcel(response, list, "药品库存表数据");
    }

    /**
     * 获取药品库存表详细信息
     */
    @RequiresPermissions("outpatient:drug:query")
    @GetMapping(value = "/{drugId}")
    public AjaxResult getInfo(@PathVariable("drugId") Long drugId)
    {
        Drug drug = drugService.selectDrugByDrugId(drugId);
        Long drugtypeId = drug.getDrugtypeId();
        Drugtype drugtype = drugtypeService.selectDrugtypeByDrugtypeId(drugtypeId);
        drug.setDrugtypeName(drugtype.getDrugtypeName());
        return success(drug);
    }

    /**
     * 新增药品库存表
     */
    @RequiresPermissions("outpatient:drug:add")
    @Log(title = "药品库存表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Drug drug)
    {
        return toAjax(drugService.insertDrug(drug));
    }

    /**
     * 修改药品库存表
     */
    @RequiresPermissions("outpatient:drug:edit")
    @Log(title = "药品库存表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Drug drug)
    {
        return toAjax(drugService.updateDrug(drug));
    }

    /**
     * 删除药品库存表
     */
    @RequiresPermissions("outpatient:drug:remove")
    @Log(title = "药品库存表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{drugIds}")
    public AjaxResult remove(@PathVariable Long[] drugIds)
    {
        return toAjax(drugService.deleteDrugByDrugIds(drugIds));
    }
}
